package com.hmdp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.Result;
import com.hmdp.dto.ShopCommentsDto;
import com.hmdp.entity.Blog;
import com.hmdp.entity.Shop;
import com.hmdp.entity.ShopComments;
import com.hmdp.mapper.ShopMapper;
import com.hmdp.service.IShopCommentsService;
import com.hmdp.utils.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/shop-comments")
public class ShopCommentsController {
    @Autowired
    private IShopCommentsService shopCommentsService;

    @Resource
    private ShopMapper shopMapper;

    @PostMapping("/saveComments")
    public Result saveComments(@RequestBody ShopComments shop){
        return shopCommentsService.saveComments(shop);
    }

    @GetMapping("/of/shop")
    public Result queryCommentsByShopId(@RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long shopId) {
        // 根据用户查询
        Page<ShopComments> page = shopCommentsService.query()
                .eq("shop_id", shopId).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<ShopComments> records = page.getRecords();
        ShopCommentsDto shopCommentsDto=new ShopCommentsDto();
        shopCommentsDto.setShopComments(records);
        shopCommentsDto.setCount(records.size());
        Shop shop=new Shop();
        shop.setId(shopId);
        shop.setComments(records.size());
        shopMapper.updateById(shop);
        return Result.ok(shopCommentsDto);
    }
}
